Imports System.Data.SqlClient
Public Class ABMBarrios
    Dim miConexion As SqlConnection
    Dim micommand As SqlCommand
    Dim cadena As String = Global.Inmobiliaria.My.Settings.inmobiliariaConnectionString
    Private Sub ABMBarrios_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.cargarCombo(Me.cmbProvincia, Me.leo_tabla("Provincias"), "idProvincia", "descripcion")
        limpiar()
    End Sub
    Private Function conectar() As Boolean
        miConexion = New SqlConnection(cadena)
        Try
            If miConexion.State = ConnectionState.Open Then
            Else
                miConexion.Open()
            End If
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function
    Private Sub cargarCombo(ByRef combo As ComboBox, ByRef datos As Data.DataTable, ByVal pk As String, ByVal descripcion As String)
        combo.ValueMember = pk
        combo.DisplayMember = descripcion
        combo.DataSource = datos
    End Sub

    Private Function leo_tabla(ByVal nombre_tabla As String) As Data.DataTable
        Dim tabla As New Data.DataTable
        If conectar() = False Then
            Me.Close()
        Else
            Me.miCommand = New SqlCommand("Select * from " & nombre_tabla, Me.miConexion)
            tabla.Load(miCommand.ExecuteReader)
        End If
        Return tabla
    End Function

    Private Sub cmbLocalidad_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbLocalidad.SelectedIndexChanged
        Me.TextBox1.Enabled = True
        Me.TextBox1.Text = ""
    End Sub

    Private Sub cmbProvincia_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbProvincia.SelectedIndexChanged
        If (Me.cmbProvincia.SelectedIndex > -1) Then
            conectar()
            Me.micommand = New SqlCommand("Select * from Localidades where idProvincia = '" & Me.cmbProvincia.SelectedValue & "'", Me.miConexion)
            Dim tabla As New Data.DataTable
            tabla.Load(micommand.ExecuteReader)
            Me.cargarCombo(Me.cmbLocalidad, tabla, "idLocalidad", "descripcion")
            Me.cmbLocalidad.SelectedIndex = -1
            Me.cmbLocalidad.Enabled = True
            Me.TextBox1.Enabled = False
            Me.TextBox1.Text = ""
        Else
            Me.cmbLocalidad.DataSource = Nothing
        End If
    End Sub
    Private Function insertar() As Boolean
        conectar()
        Dim consulta As String
        consulta = "insert into barrios (descripcion, idLocalidad)"
        consulta &= "values ('" & Me.TextBox1.Text & "', " & Me.cmbLocalidad.SelectedValue & ")"
        Me.micommand.CommandText = consulta
        Me.micommand.CommandType = CommandType.Text
        Me.micommand.Connection = Me.miConexion
        Me.micommand.ExecuteNonQuery()

        Return True
    End Function

    Private Sub btnGuardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGuardar.Click
        'validar existencia
        If Me.TextBox1.Text <> "" Then
            Me.conectar()
            Me.micommand = New SqlCommand("SELECT * FROM barrios WHERE descripcion LIKE '" & Me.TextBox1.Text & "' AND idLocalidad = " & Me.cmbLocalidad.SelectedValue, Me.miConexion)
            Dim tabla As New Data.DataTable
            tabla.Load(micommand.ExecuteReader)
            If tabla.Rows.Count = 0 Then
                insertar()
                MessageBox.Show("Se cargo satisfactoriamente.", "Importante", MessageBoxButtons.OK, MessageBoxIcon.Information)


            Else
                MessageBox.Show("El barrio ingresado ya se encuentra cargado en base de datos.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        Else
            MessageBox.Show("Por favor, ingrese un nombre de Barrio.", "Importante", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End If
    End Sub
    Private Function limpiar() As Boolean
        Me.cmbLocalidad.SelectedIndex = -1
        Me.cmbProvincia.SelectedIndex = -1
        Me.TextBox1.Text = ""
        Me.btnGuardar.Enabled = False
        Me.cmbLocalidad.Enabled = False
        Me.TextBox1.Enabled = False
        Return True
    End Function

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        If Me.TextBox1.Text <> "" Then
            Me.btnGuardar.Enabled = True
        Else
            Me.btnGuardar.Enabled = False
        End If
    End Sub

    Private Sub btnCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        limpiar()
    End Sub
End Class